<?php
/**
 * Outputs data to user on request
 *
 *
 **/

require 'tpl.php';
require 'thread.php';

if( !is_string($_REQUEST['type']) ) {
  // error BREAK
}

switch( $_REQUEST['type'] ) {
  case 'POST':
          echoPosts(); break;
  case 'THREAD':
          echoThreads(); break;
  case 'FORA':
          echoFora(); break;
  default:
}

// again, abstraction beckons us
function echoPosts() {
  if( $_REQUEST['tnum'] > 0 && $_REQUEST['start'] > 0
      && $_REQUEST['start'] < $_REQUEST['end'] ) {
    $tnum = (int)$_REQUEST['tnum'];
    $start = (int)$_REQUEST['start'];
    $end = (int)$_REQUEST['end'];

    tplHead();
    tplPostReplyButton( $post );
    // TODO we should really error check getPosts for NULL
    foreach( getPosts( $tnum, $start, $end ) as $post ) {
      tplPost( $post );
      tplPostEditButton( $post ); 
      // check to see if there is more posts and echo a pager
    }
    tplPostReplyButton( $post );
    tplFoot();
  }
}
function echoThreads() {
  if( $_REQUEST['fnum'] > 0 && $_REQUEST['start'] > 0
      && $_REQUEST['start'] < $_REQUEST['end'] ) {
    $fnum = (int)$_REQUEST['fnum'];
    $start = (int)$_REQUEST['start'];
    $end = (int)$_REQUEST['end'];

    tplHead();
    tplNewThreadButton( $fnum );
    foreach( getThreads($fnum, $start, $end) as $thread ) {
      tplThread( $thread );
    }
    tplNewThreadButton( $fnum );
    tplFoot();
  }
}
function echoFora() {
  if( $_REQUEST['start'] > 0
      && $_REQUEST['start'] < $_REQUEST['end'] ) {
    $start = (int)$_REQUEST['start'];
    $end = (int)$_REQUEST['end'];

    tplHead();
    foreach( getFora(null, $start, $end) as $forum ) {
      tplForum( $forum );
    }
    tplFoot();
  }
}

?>

